package com.sigmazero13.gameutils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.sigmazero13.gameutils.model.GameInfo;
import com.sigmazero13.gameutils.utils.AboutDialogBuilder;
import com.sigmazero13.gameutils.utils.ImportExportDatabaseFileTask;
import com.sigmazero13.gameutils.utils.TaskDoneListener;

public class GameUtilMain extends Activity implements TaskDoneListener {
	Integer numGames = 0;
	
	public static final int RANDOM_PICKER_CONFIG = 0;
	public static final int RECORD_PLAY = 1;
	public static final int GAME_TIMER = 2;
	public static final int RANDOM_PICKER_CHOICE = 3;
	public static final int IMPORT_DATABASE_FILE = 4;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		// Pick a random game
		Button pickButton = (Button)this.findViewById(R.id.button_pick_game);
		pickButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (numGames > 0) {
					Intent i = new Intent(GameUtilMain.this, GamePickerConfig.class);
					startActivityForResult(i, RANDOM_PICKER_CONFIG);
				} else {
					Toast.makeText(GameUtilMain.this, "You must have games in your collection to pick a random game...", Toast.LENGTH_LONG).show();
				}
			}
		});
		
		// Record a play
		Button recordButton = (Button)this.findViewById(R.id.button_record_play);
		recordButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (numGames > 0) {
					Intent i = new Intent(GameUtilMain.this, RecordGameConfig.class);
					startActivity(i);
				} else {
					Toast.makeText(GameUtilMain.this, "You must have games in your collection to record plays...", Toast.LENGTH_LONG).show();
				}
			}
		});
		
		// View game collection
		Button listButton = (Button)this.findViewById(R.id.button_view_games);
		listButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(GameUtilMain.this, GameList.class);
				startActivity(i);
			}
		});
		
		// View plays
		Button playsButton = (Button)this.findViewById(R.id.button_view_all_plays);
		playsButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(GameUtilMain.this, PlayList.class);
				startActivity(i);
			}
		});
		
		// Game Timer
		Button timerButton = (Button)this.findViewById(R.id.button_time_game);
		timerButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(GameUtilMain.this, GameTimerView.class);
				startActivityForResult(i, GAME_TIMER);
			}
		});
		
		// View playesr
		Button playerButton = (Button)this.findViewById(R.id.button_view_players);
		playerButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(GameUtilMain.this, PlayerList.class);
				startActivity(i);
			}
		});
	}
	
	@Override
	public void onStart() {
		super.onStart();
		updateGamesCount();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;
		switch(item.getItemId()) {
		case R.id.preferences:
			i = new Intent(GameUtilMain.this, GamePreferences.class);
			startActivity(i);
			break;
			
		case R.id.help:
			Toast.makeText(this, "Not Yet Implemented!", Toast.LENGTH_SHORT).show();
			break;
			
		case R.id.about:
			AlertDialog builder;
			try {
				builder = AboutDialogBuilder.create(this);
				builder.show();
			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}
			break;
			
		case R.id.export_db:
			new ImportExportDatabaseFileTask(this, this, null).execute();
			break;
			
		case R.id.import_id:
			i = new Intent(GameUtilMain.this, ImportDatabaseListChooser.class);
			startActivityForResult(i, IMPORT_DATABASE_FILE);
			break;
		}
		
		return true;
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        
        switch (requestCode) {
        case RANDOM_PICKER_CONFIG:	
        	switch (resultCode) {
        	case RESULT_OK:
        		Intent i = new Intent(GameUtilMain.this, GamePickerResult.class);
        		i.putExtra("num_players", intent.getExtras().getInt("num_players"));
        		i.putExtra("max_length", intent.getExtras().getInt("max_length"));
        		i.putExtra("player_names", intent.getExtras().getSerializable("player_names"));
        		startActivityForResult(i, RANDOM_PICKER_CHOICE);
        		break;
        	
        	}
        	break;
        	
        case RANDOM_PICKER_CHOICE:
        	switch (resultCode) {
        	case RESULT_OK:
        		Intent i = new Intent(GameUtilMain.this, GameTimerView.class);
        		i.putExtra("game_id", intent.getExtras().getInt("game_id"));
        		startActivityForResult(i, GAME_TIMER);
        		break;
        	}
        	break;
        	
        case GAME_TIMER:
        	switch (resultCode) {
        	case RESULT_OK:
        		Intent i = new Intent(GameUtilMain.this, RecordGameConfig.class);
        		i.putExtra("game_id", intent.getExtras().getInt("game_id"));
        		i.putExtra("game_length", intent.getExtras().getLong("game_length"));
        		startActivity(i);
        		break;
        	}
        	break;
        
        case RECORD_PLAY:
        	if (resultCode == RESULT_OK) {
        		Integer gameId = intent.getExtras().getInt("gameId");
        		GameInfo game = ((BoardGameUtilsApp)getApplication()).getDbHelper().selectSingleGame(gameId);
        		String message = "Game Not Found...";
        		if (game != null) {
        			message = "Game: " + game.getName();
        		}
        		Toast.makeText(GameUtilMain.this, message, Toast.LENGTH_SHORT).show();
        	}
        	break;
        	
        case IMPORT_DATABASE_FILE:
        	if (resultCode == RESULT_OK) {
        		String filename = intent.getExtras().getString("filename");
        		new ImportExportDatabaseFileTask(this, this, filename).execute();	
        	}
        	break;
        }
        	
    }
	
	@Override
	public void taskDone(String taskName) {
		if (taskName.equals("import")) {
			((BoardGameUtilsApp)getApplication()).refreshDb();
			updateGamesCount();
		}
	}
	
	private void updateGamesCount() {
		numGames = ((BoardGameUtilsApp)getApplication()).getDbHelper().getGameCollectionSize();
		TextView numGamesLabel = (TextView)this.findViewById(R.id.num_games_label);
		Resources res = getResources();
		String numGamesLabelStr = String.format(res.getString(R.string.num_games_string), numGames);
		numGamesLabel.setText(numGamesLabelStr);
	}
}